//没注册model-users？？？？
//const mongoose = require("mongoose");
//const User = mongoose.model("users");
//const User = require("../models/User");
const UserInfo = require("../models/UserInfo");
const keys = require("./keys");

const JwtStrategy = require("passport-jwt").Strategy,
  ExtractJwt = require("passport-jwt").ExtractJwt;
const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretOrKey;

module.exports = (passport) => {
  passport.use(
    new JwtStrategy(opts, (jwt_payload, done) => {
      //   console.log(jwt_payload);
      UserInfo.findById(jwt_payload.id)
        .then((user) => {
          if (user) {
            return done(null, user);
          } else {
            console.log("用户不存在");
            return done(null, false);
          }
        })
        .catch((err) => {
          console.log("请求头中没有token");
          return done(err, false);
        });
    })
  );
};
